Imports GalgameParser
Imports System.IO.IsolatedStorage
Imports System.Windows.Forms

''' <summary>
''' This is the main type for your game
''' </summary>
Public Class GalgamePlayerForWindows
    Inherits Microsoft.Xna.Framework.Game

    Private m_gameName As String
    Private contentManager As IsolatedContentManager
    Private m_gameParser As GalgameParser.GalgameParser

    Private m_scriptInitialization As ScriptTask
    Private m_scriptOnDraw As ScriptTask
    Private m_scriptOnUpdate As ScriptTask
    Private m_scriptOnBackKeyPress As ScriptTask

    Private WithEvents graphics As GraphicsDeviceManager
    Private WithEvents spriteBatch As SpriteBatch

    Public Sub New()
        graphics = New GraphicsDeviceManager(Me)
        Content.RootDirectory = "Content"
    End Sub

    Public Sub New(ByVal name As String)
        graphics = New GraphicsDeviceManager(Me)
        Content.RootDirectory = "Content"
        Me.m_gameName = name
    End Sub

    ''' <summary>
    ''' Allows the game to perform any initialization it needs to before starting to run.
    ''' This is where it can query for any required services and load any non-graphic
    ''' related content.  Calling MyBase.Initialize will enumerate through any components
    ''' and initialize them as well.
    ''' </summary>
    Protected Overrides Sub Initialize()
        ' TODO: Add your initialization logic here
        Me.m_gameParser = g_gameList.Where(Function(obj) obj.Name = Me.m_gameName)(0)

        Me.contentManager = New IsolatedContentManager(Content.ServiceProvider, m_gameParser.GameContentRoot, fileStorage)

        Me.m_scriptInitialization = New ScriptTask("Initialization.rb")
        m_scriptInitialization.LoadScriptFromIsolatedStorage(IO.Path.Combine(Me.contentManager.IsolatedStorageRootDirectory, "Initialization.rb"), fileStorage)

        m_scriptInitialization.LoadAssembly(Me.m_gameParser.GetType().Assembly)
        m_scriptInitialization.LoadAssembly(GetType(SpriteFont).Assembly)
        m_scriptInitialization.LoadAssembly(GetType(SpriteBatch).Assembly)
        m_scriptInitialization.LoadAssembly(GetType(GraphicsDevice).Assembly)
        m_scriptInitialization.LoadAssembly(GetType(Texture2D).Assembly)
        m_scriptInitialization.LoadAssembly(GetType(Vector2).Assembly)

        Me.m_scriptOnDraw = New ScriptTask("OnDraw.rb")
        m_scriptOnDraw.LoadScriptFromIsolatedStorage(IO.Path.Combine(Me.contentManager.IsolatedStorageRootDirectory, "OnDraw.rb"), fileStorage)

        Me.m_scriptOnDraw.LoadAssembly(Me.m_gameParser.GetType().Assembly)
        Me.m_scriptOnDraw.LoadAssembly(GetType(SpriteFont).Assembly)
        Me.m_scriptOnDraw.LoadAssembly(GetType(SpriteBatch).Assembly)
        Me.m_scriptOnDraw.LoadAssembly(GetType(GraphicsDevice).Assembly)
        Me.m_scriptOnDraw.LoadAssembly(GetType(Vector2).Assembly)
        Me.m_scriptOnDraw.LoadAssembly(GetType(Texture2D).Assembly)
        Me.m_scriptOnDraw.LoadAssembly(GetType(GameTime).Assembly)

        Me.m_scriptOnUpdate = New ScriptTask("OnUpdate.rb")
        m_scriptOnUpdate.LoadScriptFromIsolatedStorage(IO.Path.Combine(Me.contentManager.IsolatedStorageRootDirectory, "OnUpdate.rb"), fileStorage)

        Me.m_scriptOnUpdate.LoadAssembly(Me.m_gameParser.GetType().Assembly)
        Me.m_scriptOnUpdate.LoadAssembly(GetType(SpriteFont).Assembly)
        Me.m_scriptOnUpdate.LoadAssembly(GetType(SpriteBatch).Assembly)
        Me.m_scriptOnUpdate.LoadAssembly(GetType(GraphicsDevice).Assembly)
        Me.m_scriptOnUpdate.LoadAssembly(GetType(Vector2).Assembly)
        Me.m_scriptOnUpdate.LoadAssembly(GetType(Texture2D).Assembly)
        Me.m_scriptOnUpdate.LoadAssembly(GetType(GamePad).Assembly)
        Me.m_scriptOnUpdate.LoadAssembly(GetType(GameTime).Assembly)

        Me.m_scriptOnBackKeyPress = New ScriptTask("OnBackKeyPress.rb")
        m_scriptOnBackKeyPress.LoadScriptFromIsolatedStorage(IO.Path.Combine(Me.contentManager.IsolatedStorageRootDirectory, "OnBackKeyPress.rb"), fileStorage)

        Me.m_scriptOnBackKeyPress.LoadAssembly(Me.m_gameParser.GetType().Assembly)
        Me.m_scriptOnBackKeyPress.LoadAssembly(GetType(SpriteFont).Assembly)
        Me.m_scriptOnBackKeyPress.LoadAssembly(GetType(SpriteBatch).Assembly)
        Me.m_scriptOnBackKeyPress.LoadAssembly(GetType(GraphicsDevice).Assembly)
        Me.m_scriptOnBackKeyPress.LoadAssembly(GetType(Vector2).Assembly)
        Me.m_scriptOnBackKeyPress.LoadAssembly(GetType(Texture2D).Assembly)
        Me.m_scriptOnBackKeyPress.LoadAssembly(GetType(GamePad).Assembly)
        Me.m_scriptOnBackKeyPress.LoadAssembly(GetType(GameTime).Assembly)
        Me.m_scriptOnBackKeyPress.LoadAssembly(GetType(MessageBox).Assembly)

        MyBase.Initialize()
    End Sub

    ''' <summary>
    ''' LoadContent will be called once per game and is the place to load
    ''' all of your content.
    ''' </summary>
    Protected Overrides Sub LoadContent()
        ' TODO: use Me.Content to load your game content here
        m_scriptInitialization.Excute( _
            New Tuple(Of String, Object)("ThisGameParser", Me.m_gameParser), _
            New Tuple(Of String, Object)("PhonePage", Me), _
            New Tuple(Of String, Object)("ContentManager", Me.contentManager), _
            New Tuple(Of String, Object)("CurrentGraphicsDevice", GraphicsDevice))
    End Sub

    ''' <summary>
    ''' UnloadContent will be called once per game and is the place to unload
    ''' all content.
    ''' </summary>
    Protected Overrides Sub UnloadContent()
        ' TODO: Unload any non ContentManager content here
    End Sub

    ''' <summary>
    ''' Allows the game to run logic such as updating the world,
    ''' checking for collisions, gathering input, and playing audio.
    ''' </summary>
    ''' <param name="gameTime">Provides a snapshot of timing values.</param>
    Protected Overrides Sub Update(ByVal gameTime As GameTime)
        ' Allows the game to exit
        If GamePad.GetState(PlayerIndex.One).Buttons.Back = Microsoft.Xna.Framework.Input.ButtonState.Pressed Then
            Me.Exit()
        End If

        ' TODO: Add your update logic here
        Me.m_scriptOnUpdate.LoadAssembly(gameTime.GetType().Assembly)
        Me.m_scriptOnUpdate.Excute( _
            New Tuple(Of String, Object)("ThisGameParser", Me.m_gameParser), _
            New Tuple(Of String, Object)("PhonePage", Me), _
            New Tuple(Of String, Object)("GameTimerEventArg", gameTime), _
            New Tuple(Of String, Object)("CurrentGraphicsDevice", GraphicsDevice))

        MyBase.Update(gameTime)
    End Sub

    ''' <summary>
    ''' This is called when the game should draw itself.
    ''' </summary>
    ''' <param name="gameTime">Provides a snapshot of timing values.</param>
    Protected Overrides Sub Draw(ByVal gameTime As GameTime)
        GraphicsDevice.Clear(Color.CornflowerBlue)

        ' TODO: Add your drawing code here
        Me.m_scriptOnDraw.LoadAssembly(gameTime.GetType().Assembly)
        Me.m_scriptOnDraw.Excute( _
            New Tuple(Of String, Object)("ThisGameParser", Me.m_gameParser), _
            New Tuple(Of String, Object)("PhonePage", Me), _
            New Tuple(Of String, Object)("GameTimerEventArg", gameTime), _
            New Tuple(Of String, Object)("CurrentGraphicsDevice", GraphicsDevice))

        MyBase.Draw(gameTime)
    End Sub

End Class